home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / dospath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  12.2 KB  |  371 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Common operations on DOS pathnames.'''
  5. import os
  6. import stat
  7. __all__ = [
  8.     'normcase',
  9.     'isabs',
  10.     'join',
  11.     'splitdrive',
  12.     'split',
  13.     'splitext',
  14.     'basename',
  15.     'dirname',
  16.     'commonprefix',
  17.     'getsize',
  18.     'getmtime',
  19.     'getatime',
  20.     'islink',
  21.     'exists',
  22.     'isdir',
  23.     'isfile',
  24.     'ismount',
  25.     'walk',
  26.     'expanduser',
  27.     'expandvars',
  28.     'normpath',
  29.     'abspath']
  30.  
  31. def normcase(s):
  32.     """Normalize the case of a pathname.
  33.     On MS-DOS it maps the pathname to lowercase, turns slashes into
  34.     backslashes.
  35.     Other normalizations (such as optimizing '../' away) are not allowed
  36.     (this is done by normpath).
  37.     Previously, this version mapped invalid consecutive characters to a
  38.     single '_', but this has been removed.  This functionality should
  39.     possibly be added as a new function."""
  40.     return s.replace('/', '\\').lower()
  41.  
  42.  
  43. def isabs(s):
  44.     '''Return whether a path is absolute.
  45.     Trivial in Posix, harder on the Mac or MS-DOS.
  46.     For DOS it is absolute if it starts with a slash or backslash (current
  47.     volume), or if a pathname after the volume letter and colon starts with
  48.     a slash or backslash.'''
  49.     s = splitdrive(s)[1]
  50.     if s != '':
  51.         pass
  52.     return s[:1] in '/\\'
  53.  
  54.  
  55. def join(a, *p):
  56.     '''Join two (or more) paths.'''
  57.     path = a
  58.     for b in p:
  59.         if isabs(b):
  60.             path = b
  61.         elif path == '' or path[-1:] in '/\\:':
  62.             path = path + b
  63.         else:
  64.             path = path + '\\' + b
  65.     
  66.     return path
  67.  
  68.  
  69. def splitdrive(p):
  70.     '''Split a path into a drive specification (a drive letter followed
  71.     by a colon) and path specification.
  72.     It is always true that drivespec + pathspec == p.'''
  73.     if p[1:2] == ':':
  74.         return (p[0:2], p[2:])
  75.     
  76.     return ('', p)
  77.  
  78.  
  79. def split(p):
  80.     """Split a path into head (everything up to the last '/') and tail
  81.     (the rest).  After the trailing '/' is stripped, the invariant
  82.     join(head, tail) == p holds.
  83.     The resulting head won't end in '/' unless it is the root."""
  84.     (d, p) = splitdrive(p)
  85.     i = len(p)
  86.     while i and p[i - 1] not in '/\\':
  87.         i = i - 1
  88.     (head, tail) = (p[:i], p[i:])
  89.     head2 = head
  90.     while head2 and head2[-1] in '/\\':
  91.         head2 = head2[:-1]
  92.     if not head2:
  93.         pass
  94.     head = head
  95.     return (d + head, tail)
  96.  
  97.  
  98. def splitext(p):
  99.     '''Split a path into root and extension.
  100.     The extension is everything starting at the first dot in the last
  101.     pathname component; the root is everything before that.
  102.     It is always true that root + ext == p.'''
  103.     (root, ext) = ('', '')
  104.     for c in p:
  105.         if c in '/\\':
  106.             (root, ext) = (root + ext + c, '')
  107.         elif c == '.' or ext:
  108.             ext = ext + c
  109.         else:
  110.             root = root + c
  111.     
  112.     return (root, ext)
  113.  
  114.  
  115. def basename(p):
  116.     '''Return the tail (basename) part of a path.'''
  117.     return split(p)[1]
  118.  
  119.  
  120. def dirname(p):
  121.     '''Return the head (dirname) part of a path.'''
  122.     return split(p)[0]
  123.  
  124.  
  125. def commonprefix(m):
  126.     '''Return the longest prefix of all list elements.'''
  127.     if not m:
  128.         return ''
  129.     
  130.     prefix = m[0]
  131.     for item in m:
  132.         for i in range(len(prefix)):
  133.             if prefix[:i + 1] != item[:i + 1]:
  134.                 prefix = prefix[:i]
  135.                 if i == 0:
  136.                     return ''
  137.                 
  138.                 break
  139.             
  140.         
  141.     
  142.     return prefix
  143.  
  144.  
  145. def getsize(filename):
  146.     '''Return the size of a file, reported by os.stat().'''
  147.     st = os.stat(filename)
  148.     return st[stat.ST_SIZE]
  149.  
  150.  
  151. def getmtime(filename):
  152.     '''Return the last modification time of a file, reported by os.stat().'''
  153.     st = os.stat(filename)
  154.     return st[stat.ST_MTIME]
  155.  
  156.  
  157. def getatime(filename):
  158.     '''Return the last access time of a file, reported by os.stat().'''
  159.     st = os.stat(filename)
  160.     return st[stat.ST_ATIME]
  161.  
  162.  
  163. def islink(path):
  164.     """Is a path a symbolic link?
  165.     This will always return false on systems where posix.lstat doesn't exist."""
  166.     return 0
  167.  
  168.  
  169. def exists(path):
  170.     '''Does a path exist?
  171.     This is false for dangling symbolic links.'''
  172.     
  173.     try:
  174.         st = os.stat(path)
  175.     except os.error:
  176.         return 0
  177.  
  178.     return 1
  179.  
  180.  
  181. def isdir(path):
  182.     '''Is a path a dos directory?'''
  183.     
  184.     try:
  185.         st = os.stat(path)
  186.     except os.error:
  187.         return 0
  188.  
  189.     return stat.S_ISDIR(st[stat.ST_MODE])
  190.  
  191.  
  192. def isfile(path):
  193.     '''Is a path a regular file?'''
  194.     
  195.     try:
  196.         st = os.stat(path)
  197.     except os.error:
  198.         return 0
  199.  
  200.     return stat.S_ISREG(st[stat.ST_MODE])
  201.  
  202.  
  203. def ismount(path):
  204.     '''Is a path a mount point?'''
  205.     return isabs(splitdrive(path)[1])
  206.  
  207.  
  208. def walk(top, func, arg):
  209.     """Directory tree walk with callback function.
  210.  
  211.     For each directory in the directory tree rooted at top (including top
  212.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  213.     dirname is the name of the directory, and fnames a list of the names of
  214.     the files and subdirectories in dirname (excluding '.' and '..').  func
  215.     may modify the fnames list in-place (e.g. via del or slice assignment),
  216.     and walk will only recurse into the subdirectories whose names remain in
  217.     fnames; this can be used to implement a filter, or to impose a specific
  218.     order of visiting.  No semantics are defined for, or required of, arg,
  219.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  220.     a filename pattern, or a mutable object designed to accumulate
  221.     statistics.  Passing None for arg is common."""
  222.     
  223.     try:
  224.         names = os.listdir(top)
  225.     except os.error:
  226.         return None
  227.  
  228.     func(arg, top, names)
  229.     exceptions = ('.', '..')
  230.     for name in names:
  231.         if name not in exceptions:
  232.             name = join(top, name)
  233.             if isdir(name):
  234.                 walk(name, func, arg)
  235.             
  236.         
  237.     
  238.  
  239.  
  240. def expanduser(path):
  241.     """Expand paths beginning with '~' or '~user'.
  242.     '~' means $HOME; '~user' means that user's home directory.
  243.     If the path doesn't begin with '~', or if the user or $HOME is unknown,
  244.     the path is returned unchanged (leaving error reporting to whatever
  245.     function is called with the expanded path as argument).
  246.     See also module 'glob' for expansion of *, ? and [...] in pathnames.
  247.     (A function should also be defined to do full *sh-style environment
  248.     variable expansion.)"""
  249.     if path[:1] != '~':
  250.         return path
  251.     
  252.     (i, n) = (1, len(path))
  253.     while i < n and path[i] not in '/\\':
  254.         i = i + 1
  255.     if i == 1:
  256.         if not os.environ.has_key('HOME'):
  257.             return path
  258.         
  259.         userhome = os.environ['HOME']
  260.     else:
  261.         return path
  262.     return userhome + path[i:]
  263.  
  264.  
  265. def expandvars(path):
  266.     """Expand paths containing shell variable substitutions.
  267.     The following rules apply:
  268.         - no expansion within single quotes
  269.         - no escape character, except for '$$' which is translated into '$'
  270.         - ${varname} is accepted.
  271.         - varnames can be made out of letters, digits and the character '_'"""
  272.     if '$' not in path:
  273.         return path
  274.     
  275.     import string
  276.     varchars = string.ascii_letters + string.digits + '_-'
  277.     res = ''
  278.     index = 0
  279.     pathlen = len(path)
  280.     while index < pathlen:
  281.         c = path[index]
  282.         if c == "'":
  283.             path = path[index + 1:]
  284.             pathlen = len(path)
  285.             
  286.             try:
  287.                 index = path.index("'")
  288.                 res = res + "'" + path[:index + 1]
  289.             except ValueError:
  290.                 res = res + path
  291.                 index = pathlen - 1
  292.  
  293.         elif c == '$':
  294.             if path[index + 1:index + 2] == '$':
  295.                 res = res + c
  296.                 index = index + 1
  297.             elif path[index + 1:index + 2] == '{':
  298.                 path = path[index + 2:]
  299.                 pathlen = len(path)
  300.                 
  301.                 try:
  302.                     index = path.index('}')
  303.                     var = path[:index]
  304.                     if os.environ.has_key(var):
  305.                         res = res + os.environ[var]
  306.                 except ValueError:
  307.                     res = res + path
  308.                     index = pathlen - 1
  309.  
  310.             else:
  311.                 var = ''
  312.                 index = index + 1
  313.                 c = path[index:index + 1]
  314.                 while c != '' and c in varchars:
  315.                     var = var + c
  316.                     index = index + 1
  317.                     c = path[index:index + 1]
  318.                 if os.environ.has_key(var):
  319.                     res = res + os.environ[var]
  320.                 
  321.                 if c != '':
  322.                     res = res + c
  323.                 
  324.         else:
  325.             res = res + c
  326.         index = index + 1
  327.     return res
  328.  
  329.  
  330. def normpath(path):
  331.     '''Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
  332.     Also, components of the path are silently truncated to 8+3 notation.'''
  333.     path = path.replace('/', '\\')
  334.     (prefix, path) = splitdrive(path)
  335.     while path[:1] == '\\':
  336.         prefix = prefix + '\\'
  337.         path = path[1:]
  338.     comps = path.split('\\')
  339.     i = 0
  340.     while i < len(comps):
  341.         if comps[i] == '.':
  342.             del comps[i]
  343.         elif comps[i] == '..' and i > 0 and comps[i - 1] not in ('', '..'):
  344.             del comps[i - 1:i + 1]
  345.             i = i - 1
  346.         elif comps[i] == '' and i > 0 and comps[i - 1] != '':
  347.             del comps[i]
  348.         elif '.' in comps[i]:
  349.             comp = comps[i].split('.')
  350.             comps[i] = comp[0][:8] + '.' + comp[1][:3]
  351.             i = i + 1
  352.         elif len(comps[i]) > 8:
  353.             comps[i] = comps[i][:8]
  354.             i = i + 1
  355.         else:
  356.             i = i + 1
  357.     if not prefix and not comps:
  358.         comps.append('.')
  359.     
  360.     return prefix + '\\'.join(comps)
  361.  
  362.  
  363. def abspath(path):
  364.     '''Return an absolute path.'''
  365.     if not isabs(path):
  366.         path = join(os.getcwd(), path)
  367.     
  368.     return normpath(path)
  369.  
  370. realpath = abspath
  371.